﻿using System.Text;
using Kwan.Data;
using System.Collections.Specialized;

namespace BG.Web
{
    public class Callback
    {

        public void DeleteSelected(params string[] args)
        {
            string[] list = args[1].Split(',');
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Length; ++i)
            {
                if (i == 0)
                {
                    sb.Append("DELETE FROM ");
                    sb.Append(args[0]);
                    sb.Append(" WHERE");
                }
                else
                    sb.Append(" OR");
                sb.Append(" EntityID=");
                sb.Append(list[i]);
            }
            using (IConnectinContext cc = DBContext.Context1)
            {
                Command cmd = new Command(sb.ToString());
                cc.ExecuteReader(cmd);
            }
        }

        /// <summary>
        /// ChangeSelected
        /// </summary>
        /// <param name="args">0:tableName,1:obj,2:NV</param>
        public string ChangeSelected(NameValueCollection ps)
        {
            if (string.IsNullOrEmpty(ps["data"]))
            {
                //ExtHelper.GetNotification("系统提示", "请至少选择一项！").Show();
                return "{\"success\": false}";
            }
            else
            {
                string[] list = ps["data"].Split(',');
                string[] nv = ps["NV"].Split(char.Parse("_"));
                string tb = ps["tb"];
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < list.Length; ++i)
                {
                    if (i == 0)
                    {
                        sb.Append("UPDATE ZP_");
                        sb.Append(ps["tb"]);
                        sb.Append(" SET ");
                        sb.Append(nv[0]);
                        sb.Append(" = ");
                        sb.Append(nv[1]);
                        sb.Append(" WHERE EntityID=");
                        sb.Append(list[i]);
                        //sb.Append(";");
                    }
                    else
                    {
                        sb.Append(" OR  EntityID=");
                        sb.Append(list[i]);
                    }
                }
                using (IConnectinContext cc = DBContext.Context1)
                {
                    Command cmd = new Command(sb.ToString());
                    cc.ExecuteReader(cmd);
                }
                //return "{\"success\": true}";
                return "success";
            }
        }
    }
}
